package com.kevin.hw.zkclient;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: kevin
 * @date: 2017/8/11
 * @description:
 */
public class ZkClientApiOperator {


    private final static String CONNECT_HOST = "localhost:2181";

    private static ZkClient getInstant(){
        return new ZkClient(CONNECT_HOST,5000);
    }


    public static void main(String[] args) throws InterruptedException {

        ZkClient zkClient = getInstant();

        List<String> list = zkClient.getChildren("/");

        System.out.println("list ->" + list);

        zkClient.subscribeDataChanges("/node1", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println("节点名称："+ s + ",节点修改后的值：->"+o);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println("节点删除的名称：——>"+s);
            }
        });



//        zkClient.writeData("/node1","11");


//        zkClient.delete("/node1");

//        TimeUnit.SECONDS.sleep(5);




        zkClient.subscribeChildChanges("/node1", new IZkChildListener() {
            @Override
            public void handleChildChange(String s, List<String> list) throws Exception {
                System.out.println("修改的节点名称:"+s);
            }
        });

        TimeUnit.SECONDS.sleep(5);


        zkClient.create("/node1/node1-2", "333", CreateMode.PERSISTENT);

        TimeUnit.SECONDS.sleep(10);

    }

}
